<?php
function detail_kelompok_shift( $karyawan_id,$periode_id   ){
	my_set_code_js('
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&task=pindahperiode&karyawan_id='.$karyawan_id.'&periode_id=\'+periode_id ;
		}
	');
	$periode_absen_id =get_periode_absen();
	if($periode_id==0){
		$periode_id =get_last_periode_id($periode_absen_id);
	}	
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);	
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$karyawan =  loaddata_karyawan($karyawan_id,false); 
	$kelompok_shift_id =$karyawan['kelompok_shift_id'];
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}
	
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'S. Sts'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'S. Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:23%'),   
		'RS'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'PC'=>array('style'=>'text-align:left;border-bottom:2px solid;width:14%'),   
		'R Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:23%'),   
		'M'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
		'P'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
	);
	$row= array();
	foreach($tanggals as $tanggal){	
		$areals=get_realisasi($tanggal , $karyawan_id, $periode_id ) ;
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$areals['s_jadwal'], 
				's_time' 	=>$areals['sc_time'], 
				'r_status' 	=>$areals['r_status'], 
				'r_pg' 		=>$areals['pc'], 
				'r_time'	=>$areals['r_time'], 
				'chk1'		=>position_text_align( ($areals['flag_masuk']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
				'chk2'		=>position_text_align( ($areals['flag_pulang']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$areals['s_jadwal'], 
				's_time' 	=>$areals['sc_time'], 
				'r_status' 	=>$areals['r_status'], 
				'r_pg' 		=>$areals['pc'], 
				'r_time'	=>$areals['r_time'], 
				'chk1'		=>position_text_align( ($areals['flag_masuk']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
				'chk2'		=>position_text_align( ($areals['flag_pulang']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),   
			);
		}	
	}
	$datas = table_rows($row);
	$table1 = table_builder($header , $datas ,  2 , false);
	
	$tanggals = list_kalender($middledata2 ,$enddate  );
	$tanggals = array_unique($tanggals) ;
	$row= array(); 
	foreach($tanggals as $tanggal){
		$areals=get_realisasi($tanggal , $karyawan_id, $periode_id ) ;
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$areals['s_jadwal'], 
				's_time' 	=>$areals['sc_time'], 
				'r_status' 	=>$areals['r_status'], 
				'r_pg' 		=>$areals['pc'], 
				'r_time'	=>$areals['r_time'], 
				'chk1'		=>position_text_align( ($areals['flag_masuk']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
				'chk2'		=>position_text_align( ($areals['flag_pulang']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$areals['s_jadwal'], 
				's_time' 	=>$areals['sc_time'], 
				'r_status' 	=>$areals['r_status'], 
				'r_pg' 		=>$areals['pc'], 
				'r_time'	=>$areals['r_time'], 
				'chk1'		=>position_text_align( ($areals['flag_masuk']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),  
				'chk2'		=>position_text_align( ($areals['flag_pulang']=="Y"  ? button_icon( 'check2.png' ,'') :  '') , 'center'),   
			);
		}	
	}
	$datas = table_rows($row);
	$table2 = table_builder($header , $datas ,  2 , false);
 
	$query_check_periode = " SELECT * FROM pr_periode WHERE periode_id< {$periode_absen_id} ORDER BY periode_id DESC";	
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('Y-m',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$form_dpwerio = form_dropdown($fperiode_id,$prs);
	
	$newline = array(
		'Periode'=>$form_dpwerio ,
		'Tipe Waktu Kerja'=>$karyawan['waktu_kerja'],
		'Kelompok'=>$karyawan['kelompok'],
	);
	  
	$view = karyawan_header_detail($karyawan  ,  true , $newline );
	 
	$split ='<div style="height:470px;"><form method="post" name="multiDataBox">
	<div style="width:49%;float:left">'.$table1.'</div>';
	$split .='<div style="width:49%;float:right">'.$table2.'</div></div>';
	$view .= '<div style="width:98%;text-align:right;">
	<!-- input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=rpt_wk_realisasi&karyawan_id='.$karyawan_id.'&'.rand(0,1111).'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/ -->
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=realisasi_wk_karyawan&task=absenperkaryawan&karyawan_id='.$karyawan_id.'\'"/>
	</form></div>'   ; 	
	return $view . $split;
}

function get_realisasi($tanggal , $karyawan_id, $periode_id ){
	$datas=array();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);	
	$ch = "SELECT * FROM a_realisasi_log 
			WHERE karyawan_id = {$karyawan_id} 
			AND field_tgl='tgl_{$dd}' 
			AND periode_id={$periode_id} 
			ORDER BY realisasi_log_id 
			DESC LIMIT 1";
	$chr = my_query($ch);
	$areals = my_fetch_array($chr);	
	$sc_jadwal_kerja_kelompok_id= isset( $areals['sc_jadwal_kerja_kelompok_id'] ) ? $areals['sc_jadwal_kerja_kelompok_id'] :  0;
	$s_jadwal=get_jadwal_kerja_kelompok($sc_jadwal_kerja_kelompok_id);
	$datas['s_jadwal']=$s_jadwal ;
	$datas['sc_time']= $areals['sc_time']  ;
	$status_absen_id= isset( $areals['status_absen_id'] ) ? $areals['status_absen_id'] :  0;
	$statuss=my_get_data_by_id('a_status_absen','status_absen_id',$status_absen_id) ;
	$datas['r_status']= $statuss['initial_absen']  ;
	
	$pc=$areals['pc'] ;
	if($pc=='00:00:00'){
		$pc='';
	}	
	$pc=(trim($pc)<>'') ? date('H:i',strtotime($pc)) :'' ;
	$datas['pc']=$pc ;	
	
	$jam_datang=$areals['jam_datang'] ;
	if($jam_datang=='00:00:00'){
		$jam_datang='';
	}	
	$jam_datang=(trim($jam_datang)<>'') ? date('H:i',strtotime($jam_datang)) :'';

	$jam_pulang=$areals['jam_pulang'] ;
	if($jam_pulang=='00:00:00'){
		$jam_pulang='';
	}	
	$jam_pulang=(trim($jam_pulang)<>'') ? date('H:i',strtotime($jam_pulang)) :'';	
	$datas['r_time']=$jam_datang.'-'.$jam_pulang ;	
	$datas['flag_masuk']= $areals['flag_masuk']  ;
	$datas['flag_masuk']= $areals['flag_masuk']  ;
	return $datas;	
}

function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}

function get_jadwal_kerja_kelompok($sc_jadwal_kerja_kelompok_id){
	if($sc_jadwal_kerja_kelompok_id==21){
		return "X" ;
	}	
	$jadwal_kerja_kelompoks = my_get_data_by_id('a_jadwal_kerja_kelompok','jadwal_kerja_kelompok_id',$sc_jadwal_kerja_kelompok_id);
	$waktu_kerja_id=isset( $jadwal_kerja_kelompoks['waktu_kerja_id'] ) ? $jadwal_kerja_kelompoks['waktu_kerja_id'] :  0 ;
	$jadwal_kerjas = my_get_data_by_id('a_waktu_kerja','waktu_kerja_id',$waktu_kerja_id);
	return $jadwal_kerjas['label'].'/'.$jadwal_kerja_kelompoks['kelompok'] ;
}